Passed
Push — master ( 5ff06a...d1d19e )
by Rafael S.
01:41
created

write-bytes.js ➔ write64BitFloat   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 5
rs 9.4285
1
/*
2
 * write-bytes: Functions to turn data into bytes.
3
 * Copyright (c) 2017 Rafael da Silva Rocha.
4
 * https://github.com/rochars/byte-data
5
 */
6
7
const floats = require("../src/floats.js");
8
const intBits = require("int-bits");
9
10
function write64BitFloat(bytes, numbers, i, j) {
11
    let bits = floats.toFloat64(numbers[i]);
12
    j = write32Bit(bytes, bits, 1, j);
13
    return write32Bit(bytes, bits, 0, j);
14
}
15
16
// https://github.com/majimboo/c-struct
17
function write48Bit(bytes, numbers, i, j) {
18
    bytes[j++] = numbers[i] & 0xFF;
19
    bytes[j++] = numbers[i] >> 8 & 0xFF;
20
    bytes[j++] = numbers[i] >> 16 & 0xFF;
21
    bytes[j++] = numbers[i] >> 24 & 0xFF;
22
    bytes[j++] = numbers[i] / 0x100000000 & 0xFF;
23
    bytes[j++] = numbers[i] / 0x10000000000 & 0xFF;
24
    return j;
25
}
26
27
// https://github.com/majimboo/c-struct
28
function write40Bit(bytes, numbers, i, j) {
29
    bytes[j++] = numbers[i] & 0xFF;
30
    bytes[j++] = numbers[i] >> 8 & 0xFF;
31
    bytes[j++] = numbers[i] >> 16 & 0xFF;
32
    bytes[j++] = numbers[i] >> 24 & 0xFF;
33
    bytes[j++] = numbers[i] / 0x100000000 & 0xFF;
34
    return j;
35
}
36
37
function write32BitFloat(bytes, numbers, i, j) {
38
    let bits = intBits.unpack(numbers[i]);
39
    bytes[j++] = bits & 0xFF;
40
    bytes[j++] = bits >>> 8 & 0xFF;
41
    bytes[j++] = bits >>> 16 & 0xFF;
42
    bytes[j++] = bits >>> 24 & 0xFF;
43
    return j;
44
}
45
46
function write32Bit(bytes, numbers, i, j) {
47
    bytes[j++] = numbers[i] & 0xFF;
48
    bytes[j++] = numbers[i] >>> 8 & 0xFF;
49
    bytes[j++] = numbers[i] >>> 16 & 0xFF;
50
    bytes[j++] = numbers[i] >>> 24 & 0xFF;
51
    return j;
52
}
53
54
function write24Bit(bytes, numbers, i, j) {
55
    bytes[j++] = numbers[i] & 0xFF;
56
    bytes[j++] = numbers[i] >>> 8 & 0xFF;
57
    bytes[j++] = numbers[i] >>> 16 & 0xFF;
58
    return j;
59
}
60
61
function write16Bit(bytes, numbers, i, j) {
62
    bytes[j++] = numbers[i] & 0xFF;
63
    bytes[j++] = numbers[i] >>> 8 & 0xFF;
64
    return j;
65
}
66
67
function write16BitFloat(bytes, numbers, i, j) {
68
    let bits = floats.toHalf(numbers[i]);
69
    bytes[j++] = bits  >>> 8 & 0xFF;
70
    bytes[j++] = bits  & 0xFF;
71
    return j;
72
}
73
74
function write8Bit(bytes, numbers, i, j) {
75
    bytes[j++] = numbers[i] & 0xFF;
76
    return j;
77
}
78
79
function write4Bit(bytes, numbers, i, j) {
80
    bytes[j++] = numbers[i] & 0xF;
81
    return j;
82
}
83
84
function write2Bit(bytes, numbers, i, j) {
85
    bytes[j++] = numbers[i] < 0 ? numbers[i] + 4 : numbers[i];
86
    return j;
87
}
88
89
function write1Bit(bytes, numbers, i, j) {
90
    bytes[j++] = numbers[i] ? 1 : 0;
91
    return j;
92
}
93
94
function writeString(bytes, string, i, j) {
95
    bytes[j++] = string.charCodeAt(i);
96
    return j;
97
}
98
99
module.exports.write64BitFloat = write64BitFloat;
100
module.exports.write48Bit = write48Bit;
101
module.exports.write40Bit = write40Bit;
102
module.exports.write32BitFloat = write32BitFloat;
103
module.exports.write32Bit = write32Bit;
104
module.exports.write24Bit = write24Bit;
105
module.exports.write16Bit = write16Bit;
106
module.exports.write16BitFloat = write16BitFloat;
107
module.exports.write8Bit = write8Bit;
108
module.exports.write4Bit = write4Bit;
109
module.exports.write2Bit = write2Bit;
110
module.exports.write1Bit = write1Bit;
111
module.exports.writeString = writeString;
112